"""
CLI entry for Problem 1.

You can choose occlusion strategy via CLI arguments:
- point (default): representative point (center/top/bottom)
- sample: cylinder sampling with configurable density and threshold

Examples:
    python -m src.main_q1 --strategy point --which top_center
    python -m src.main_q1 --strategy sample --n_circ 72 --n_h 9 --threshold 1.0
"""

from __future__ import annotations
import argparse
from src.core.solver_qi_ana import Q1Config, solve_q1_analytic_aggregate
from src.core.occlusion.base import PointLoSStrategy, CylinderSamplingStrategy


def build_parser() -> argparse.ArgumentParser:
    p = argparse.ArgumentParser(description="Problem 1 solver with pluggable occlusion strategies")
    p.add_argument("--strategy", choices=["point", "sample"], default="point")
    p.add_argument("--which", choices=["center", "top_center", "bottom_center"], default="center",
                   help="Representative point selection (only for --strategy point)")
    p.add_argument("--n_circ", type=int, default=36, help="Sampling points on circumference (only for sample)")
    p.add_argument("--n_h", type=int, default=5, help="Number of height layers (only for sample)")
    p.add_argument("--threshold", type=float, default=1.0, help="Blocked fraction threshold (only for sample)")
    p.add_argument("--dt", type=float, default=0.005, help="Time step for scanning")
    p.add_argument("--refine", type=int, default=60, help="Bisection iterations for edge refinement")
    return p


def main() -> None:
    args = build_parser().parse_args()

    cfg = Q1Config(dt_scan=args.dt, refine_iters=args.refine)

    if args.strategy == "point":
        strategy = PointLoSStrategy(which_point=args.which)
    else:
        strategy = CylinderSamplingStrategy(n_circ=args.n_circ, n_h=args.n_h, threshold=args.threshold)

    res = solve_q1_analytic_aggregate(cfg)
    print(f"Strategy: {type(strategy).__name__}")
    print(f"Detonation: t_det={res['t_det']:.3f}s, xy_det={res['xy_det']}, z_det={res['z_det']:.3f} m")
    print("Cover intervals (s):", [(round(a, 3), round(b, 3)) for a, b in res['intervals']])
    print("Total cover time (s):", round(res['total_cover'], 3))


if __name__ == "__main__":
    main()